using System;
using Commons.Utils;

namespace Commons.Models.TradeValues
{
	[Serializable]
	public class ProbabilityTradeValue : TradeValue
	{
		public const string ContextRewardKey = "probabilityReward";

		private RangeDictionary<int, ITradeValue> ranges = new RangeDictionary<int, ITradeValue>(true);
		private int totalProbability = 0;

		public ProbabilityTradeValue ()
			: base("probability")
		{
			
		}

		public void AddValue(int probability, ITradeValue value)
		{
			if (totalProbability + probability > RandomHelper.MaxProbability)
			{
                throw new ArgumentException("Total probability cannot be greater than " + RandomHelper.MaxProbability, "probability");
			}
			ranges.Add(totalProbability, value);
			totalProbability += probability;
		}

		public ITradeValue GetValue(int probability)
		{
			if (probability >= totalProbability)
			{
				return null;
			}

			return ranges.Get(probability);
		}
	}
}

